﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using Entidades;
using Controladores;
using Web.Helpers.jqGridHelpers;
using Common;

namespace Web.Helpers
{
    public class GridData<T> where T : Entidad
    {
        #region Properties

        private int _TotalRecords;
        /// <summary>
        /// Total Records
        /// </summary>
        public int TotalRecords
        {
            get { return _TotalRecords; }
            set { _TotalRecords = value; }
        }

        private int _TotalPages;
        /// <summary>
        /// Total Pages
        /// </summary>
        public int TotalPages
        {
            get { return _TotalPages; }
            set { _TotalPages = value; }
        }

        private int _CurrentPage;
        /// <summary>
        /// Current Page
        /// </summary>
        public int CurrentPage
        {
            get { return _CurrentPage; }
            set { _CurrentPage = value; }
        }

        private List<T> _Data;
        /// <summary>
        /// Data
        /// </summary>
        public List<T> Data
        {
            get { return _Data; }
            set { _Data = value; }
        }

        #endregion
    }

    public class GridDataHelper
    {
        public static Common.Data.ParameterList GetParameters(string searchFilter, string colModel)
        {
            var parameterList   = new Common.Data.ParameterList();
            var jqSearchFilter  = new jqGridSearchFilterBuilder(searchFilter, colModel);
            //var filter = jqGridFilter.ConvertFromJSON(searchFilter);
            //foreach (var rule in filter.rules)
            foreach(var ruleSet in jqSearchFilter.RuleSets)
            {
                foreach (var rule in ruleSet.Rules)
                {
                    var field       = rule.Field;
                    var data        = rule.Data;
                    var columnType  = jqSearchFilter.ColumnsInfo[field];
                    var value       = ConvertValue(columnType, data);

                    parameterList.AddParameter(field, value);
                }
            }
            return parameterList;
        }

        private static object ConvertValue(Type columnType, string data)
        {
            if (columnType == typeof(Boolean))
                //return Convert.ToBoolean(data.ToLower());
                return Common.Formatter.ToBoolean(data.ToLower());

            if (columnType == typeof(Int32))
                return Convert.ToInt32(data);

            if (columnType == typeof(Int64))
                return Convert.ToInt64(data);

            if (columnType == typeof(Decimal))
                return Convert.ToDecimal(data);

            if (columnType == typeof(Double))
                return Convert.ToDouble(data);

            if (columnType == typeof(Guid))
                return new Guid(data);

            if (columnType == typeof(DateTime))
                return Convert.ToDateTime(data);

            if (columnType == typeof(String))
                return data;

            return null;
        }


        public GridData<Entidades.Entidad> GetPaginationData(int totalRecords, int currentPage, int pageSize)
        {
            var data = new GridData<Entidades.Entidad>();
            try
            {
                data.TotalRecords = totalRecords;
                data.TotalPages = (int)Math.Ceiling((decimal)totalRecords / (decimal)pageSize);
                data.CurrentPage = currentPage;
            }
            catch (Exception)
            {
                throw;
            }
            return data;
        }
    }

    public class GridDataHelper<T> where T : Entidad
    {
        #region Constructors

        /// <summary>
        /// Constructor default
        /// </summary>
        public GridDataHelper()
        {
        }

        #endregion

        #region Methods

        public GridData<T> GetData(string searchFilter, string colModel, int currentPage, int pageSize, string sortOrder, string sortColumn)
        {
            var controlador = new ControladorBase<T>();
            var data        = new GridData<T>();
            try
            {
                var filtro          = new jqGridSearchFilterBuilder(searchFilter, colModel).ToQuery();
                var totalRecords    = controlador.ObtenerTotalEntidades(filtro);
                var totalPages      = (int)Math.Ceiling((decimal)totalRecords / (decimal)pageSize);
                var paginacion      = new Data.Pagination(totalRecords, totalPages, currentPage, pageSize, sortOrder, sortColumn);
                var listaEntidades  = controlador.ObtenerEntidades(paginacion, filtro);

                data.TotalPages     = totalPages;
                data.TotalRecords   = totalRecords;
                data.CurrentPage    = currentPage;
                data.Data           = listaEntidades;
            }
            catch (Exception)
            {
                throw;
            }
            return data;
        }

        #endregion
    }
}